/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package energyDown.cenario.validacao;

import energyDown.gui.DialogEdicaoAparelho;
import energyDown.gui.LabelAparelho;
import energyDown.modelo.Cenario;
import energyDown.modelo.unidade.Conforto;
import energyDown.modelo.unidade.FimDeJogo;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Ana Meireles
 */
public class PnlCenarioValidacao extends javax.swing.JPanel implements MouseListener {

    private Cenario cenario;
    private JFrame rootFrame;
    private HashMap<Conforto, ImageIcon> mapaConfortoIcone = new HashMap<Conforto, ImageIcon>();

    /**
     * Creates new form PnlCenarioValidação
     */
    public PnlCenarioValidacao(JFrame rootFrame) {
        initComponents();
        iniciarInterface();
        iniciarMouseListeners();
        this.rootFrame = rootFrame;
        
        ImageIcon imgOtimo = new ImageIcon(getClass().getResource("/energyDown/gui/iconOtimo.png"));
        mapaConfortoIcone.put(Conforto.OTIMO, imgOtimo);
        ImageIcon imgBom = new ImageIcon(getClass().getResource("/energyDown/gui/iconBom.png"));
        mapaConfortoIcone.put(Conforto.BOM, imgBom);
        ImageIcon imgRegular = new ImageIcon(getClass().getResource("/energyDown/gui/iconRegular.png"));
        mapaConfortoIcone.put(Conforto.REGULAR, imgRegular);
        ImageIcon imgRuim = new ImageIcon(getClass().getResource("/energyDownNow/gui/iconRuim.png"));
        mapaConfortoIcone.put(Conforto.RUIM, imgRuim);
        ImageIcon imgPessimo = new ImageIcon(getClass().getResource("/energyDownNow/gui/iconPessimo.png"));
        mapaConfortoIcone.put(Conforto.PESSIMO, imgPessimo);
        atualizarLabelRosto();
    }

    private void iniciarInterface() {
        cenario = CenarioValidacao.getCenario();

        labelAparelho1.setAparelho(cenario.getUsoAparelhos().get(0));
        labelAparelho2.setAparelho(cenario.getUsoAparelhos().get(1));
        labelAparelho3.setAparelho(cenario.getUsoAparelhos().get(2));
        labelAparelho4.setAparelho(cenario.getUsoAparelhos().get(3));
        labelAparelho5.setAparelho(cenario.getUsoAparelhos().get(4));
        labelAparelho6.setAparelho(cenario.getUsoAparelhos().get(5));
        labelAparelho7.setAparelho(cenario.getUsoAparelhos().get(6));
        labelAparelho8.setAparelho(cenario.getUsoAparelhos().get(7));
        labelAparelho9.setAparelho(cenario.getUsoAparelhos().get(8));

        
        painelStatusCenario1.setCenario(cenario);
        painelStatusCenario1.atualizar();
    }

    private void iniciarMouseListeners() {
        labelAparelho1.addMouseListener(this);
        labelAparelho2.addMouseListener(this);
        labelAparelho3.addMouseListener(this);
        labelAparelho4.addMouseListener(this);
        labelAparelho5.addMouseListener(this);
        labelAparelho6.addMouseListener(this);
        labelAparelho7.addMouseListener(this);
        labelAparelho8.addMouseListener(this);
        labelAparelho9.addMouseListener(this);
    }
    
    private void atualizarLabelRosto(){
        Conforto confortoAna = cenario.getConfortoPersonagem("Ana");
        lblRosto.setIcon( mapaConfortoIcone.get(confortoAna) );
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("EnergyDownNow - Cenário de Teste");
        PnlCenarioValidacao painel = new PnlCenarioValidacao(frame);
        frame.add(painel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelAparelho1 = new energyDown.gui.LabelAparelho();
        labelAparelho2 = new energyDown.gui.LabelAparelho();
        labelAparelho3 = new energyDown.gui.LabelAparelho();
        labelAparelho4 = new energyDown.gui.LabelAparelho();
        labelAparelho5 = new energyDown.gui.LabelAparelho();
        labelAparelho6 = new energyDown.gui.LabelAparelho();
        labelAparelho7 = new energyDown.gui.LabelAparelho();
        labelAparelho8 = new energyDown.gui.LabelAparelho();
        labelAparelho9 = new energyDown.gui.LabelAparelho();
        painelStatusCenario1 = new energyDown.gui.PainelStatusCenarios();
        jButton1 = new javax.swing.JButton();
        lblRosto = new javax.swing.JLabel();

        labelAparelho1.setText("Aparelho1");

        labelAparelho2.setText("Aparelho2");

        labelAparelho3.setText("Aparelho3");

        labelAparelho4.setText("Aparelho4");

        labelAparelho5.setText("Aparelho5");

        labelAparelho6.setText("Aparelho6");

        labelAparelho7.setText("Aparelho7");

        labelAparelho8.setText("Aparelho8");

        labelAparelho9.setText("Aparelho9");

        painelStatusCenario1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jButton1.setText("Avançar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        lblRosto.setText("Ana");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelAparelho3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelAparelho1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelAparelho5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelAparelho7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelAparelho2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelAparelho4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelAparelho6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelAparelho8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelAparelho9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 219, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addContainerGap(209, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblRosto)
                            .addComponent(painelStatusCenario1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelAparelho1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(labelAparelho2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelAparelho3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelAparelho4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelAparelho5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelAparelho6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelAparelho7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelAparelho8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelAparelho9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(painelStatusCenario1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRosto)
                        .addGap(35, 35, 35)
                        .addComponent(jButton1)))
                .addContainerGap(63, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        cenario.avancar();
        painelStatusCenario1.atualizar();
        atualizarLabelRosto();

        if (cenario.getFimDeJogo() == FimDeJogo.METAS_ATINGIDAS) {

            JOptionPane.showMessageDialog(this, "Parabéns! Você atingiu as metas! :D\n"
                    + "Consumo do cenário em KWh =  " + cenario.calcularConsumoEmKWh() + " KWh.\n"
                    + "Consumo atual do cenário = R$ " + cenario.getUltimaDespesa(),
                    "Resultado do Cenário", JOptionPane.INFORMATION_MESSAGE);
            iniciarInterface();

        } else if (cenario.getFimDeJogo() == FimDeJogo.FIM_DO_PRAZO) {

            JOptionPane.showMessageDialog(this, "Você não atingiu as metas... :(\n"
                    + "Consumo do cenário em KWh =  " + cenario.calcularConsumoEmKWh() + " KWh.\n"
                    + "Consumo atual do cenário = R$ " + cenario.getUltimaDespesa(),
                    "Resultado do Cenário", JOptionPane.INFORMATION_MESSAGE);
            iniciarInterface();

        }
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private energyDown.gui.LabelAparelho labelAparelho1;
    private energyDown.gui.LabelAparelho labelAparelho2;
    private energyDown.gui.LabelAparelho labelAparelho3;
    private energyDown.gui.LabelAparelho labelAparelho4;
    private energyDown.gui.LabelAparelho labelAparelho5;
    private energyDown.gui.LabelAparelho labelAparelho6;
    private energyDown.gui.LabelAparelho labelAparelho7;
    private energyDown.gui.LabelAparelho labelAparelho8;
    private energyDown.gui.LabelAparelho labelAparelho9;
    private javax.swing.JLabel lblRosto;
    private energyDown.gui.PainelStatusCenarios painelStatusCenario1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void mouseClicked(MouseEvent e) {
        Object origem = e.getSource();
        if (origem instanceof LabelAparelho) {
            LabelAparelho label = (LabelAparelho) origem;
            DialogEdicaoAparelho dialog = new DialogEdicaoAparelho(rootFrame, label.getAparelho());
            dialog.setLocationRelativeTo(rootFrame);
            dialog.setVisible(true);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
